﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;

namespace Shoy.Wcf.Attributes
{
    public class WcfExceptionHandler : IErrorHandler
    {
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            FaultException newEx;
            if (error is ArgumentException)
                newEx = new FaultException(error.Message);
            else
            {
                var err = $"调用WCF接口{error.TargetSite.Name} - '{error.TargetSite.Name}' 出错，详情：{error.Message}";
                newEx = new FaultException(error.Message);//new FaultException("系统内部错误"); 
            }
            var msgFault = newEx.CreateMessageFault();
            fault = Message.CreateMessage(version, msgFault, newEx.Action);
        }

        public bool HandleError(Exception error)
        {
            return true;
        }
    }
}
